package com.tanhua.app.Service;

import com.alibaba.fastjson.JSON;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void checkUserFreeze(Long userId,String state){
        String value = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
        if(StringUtils.isNotBlank(value)){
            Map map = JSON.parseObject(value, Map.class);
            String freezingRange = map.get("freezingRange").toString();
            if(state.equals(freezingRange)&&state.equals("1")){
                throw new BusinessException(ErrorResult.freezeLoginError());
            }
            if(state.equals(freezingRange)&&state.equals("2")){
                throw new BusinessException(ErrorResult.freezeSpeakError());
            }
            if(state.equals(freezingRange)&&state.equals("3")){
                throw new BusinessException(ErrorResult.freezeMovementError());
            }

        }
    }
}
